import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_widgets_pc/widgets/chart/kq_chart_bar_model.dart';

import 'kq_chart_line_model.dart';
import 'kq_chart_model.dart';

class KqChartBar extends StatefulWidget {
  ///内部参数设置
  final KqChartBarModel chartModel;

  ///控住多图刷新的key，同一页面有多个图是必填
  final String? keyTag;

  ///背景色
  final Color? backGroundColor;

  ///总高度
  final double? height;

  ///总宽度
  final double? width;

  final List<List<ChartBarValue>>? barList;
  final List<List<ChartLineValue>>? lineList;

  const KqChartBar(
      {Key? key,
      required this.chartModel,
      this.keyTag,
      this.backGroundColor,
      this.height,
      this.width,
      this.barList,
      this.lineList})
      : super(key: key);

  @override
  State<KqChartBar> createState() => _KqChartBarState();
}

class _KqChartBarState extends State<KqChartBar>
    with SingleTickerProviderStateMixin {
  late Animation<double> animation;
  late AnimationController controller;
  late KqChartBarModel barModel;

  @override
  void initState() {
    super.initState();
    barModel = Get.put(widget.chartModel, tag: widget.keyTag);
    barModel.setValueList(widget.barList ?? widget.chartModel.chartBarArry,
        lineList: widget.lineList ?? widget.chartModel.chartLineArry);
    controller = AnimationController(
      duration: barModel.chartModel.duration,
      vsync: this,
    );
    animation = Tween(begin: 0.0, end: 1.0).animate(controller)
      ..addListener(() {
        barModel.chartModel.animationTime = animation.value;
        barModel.update();
      })
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          barModel.chartModel.isAnimationEnd = true;
          barModel.update();
        }
      });
    //启动动画(正向执行)
  }

  KqBarChartPainter getLineChart() {
    KqChartModel model = barModel.chartModel;
    KqBarChartPainter painter = KqBarChartPainter(
        yStyle: TextStyle(fontSize: model.yFont, color: model.yColor),
        xStyle: TextStyle(fontSize: model.xFont, color: model.xColor),
        maxY: model.maxY,
        lines: barModel.chartLineArry,
        bars: barModel.chartBarArry,
        xList: barModel.xList,
        xValue: barModel.xValue,
        xRinght: model.xRight,
        yList: barModel.yList,
        yValue: barModel.yValue,
        isDivide: model.isDivide,
        axisColor: model.axisColor!,
        dividerColor: model.dividerColor!,
        isCurve: model.isCurve,
        animationTime: model.animationTime,
        isShowPoint: model.isShowPoint,
        pointRadius: model.pointRadius,
        isShowValue: model.isShowValue,
        hintLineSolid: model.hintLineSolid,
        isAnimationEnd: barModel.chartModel.isAnimationEnd);
    return painter;
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

class KqBarChartPainter extends CustomPainter {
  ///动画是否结束
  final bool isAnimationEnd;

  ///轴线颜色
  final Color axisColor;

  ///分割线颜色
  final Color dividerColor;

  ///分割线是否为实现
  final bool hintLineSolid;

  ///是否展示分割线,默认为true
  final bool isDivide;

  ///是否曲线
  final bool isCurve;

  ///Y轴文案样式
  final TextStyle yStyle;

  ///X轴文案样式
  final TextStyle xStyle;

  ///x轴坐标数组
  final List xList;

  ///y轴坐标数组
  final List yList;

  ///横坐标内容
  List xValue;

  ///纵坐标内容
  List yValue;

  ///x轴距离左边距离;
  final double xRinght;

  ///最大值
  final double maxY;

  ///是否显示内容坐标点
  bool isShowPoint;

  ///是否显示全部的内容文案
  bool isShowValue;

  ///内容坐标点的半径
  double pointRadius;

  ///具体数据
  final List<List<ChartLineValue>>? lines;
  final List<List<ChartBarValue>> bars;

  ///动画进度
  final double animationTime;

  List<BarCanvasModel> lineModels = [];

  KqBarChartPainter({
    Key? key,
    required this.isAnimationEnd,
    required this.yStyle,
    required this.xStyle,
    required this.isCurve,
    required this.maxY,
    required this.axisColor,
    required this.dividerColor,
    required this.isDivide,
    required this.xList,
    required this.yList,
    required this.xValue,
    required this.yValue,
    required this.xRinght,
    required this.animationTime,
    this.lines,
    required this.isShowPoint,
    required this.pointRadius,
    required this.isShowValue,
    required this.hintLineSolid,
    required this.bars,
  }) : super();

  @override
  void paint(Canvas canvas, Size size) {}

  @override
  bool shouldRepaint(covariant KqBarChartPainter oldDelegate) {
    return true;
  }
}

class BarCanvasModel {
  ///遮罩层路径
  Path shadowPaths;

  ///折线或曲线路径
  Path path;

  ///点的数组
  int points;

  ///线的颜色
  Color lineCorlor;

  ///柱状颜色
  Color barColor;

  BarCanvasModel({
    required this.points,
    required this.shadowPaths,
    required this.lineCorlor,
    required this.path,
    required this.barColor,
  });
}
